/*
 * 说明:
 * superinput - 整个模拟input的div的id
 * active     - 真正input的id
 * suggest    - 好友suggest的div的id
 * suggest_i  - 好友suggest的div的子div的id
 * emptysuggest - 无好友suggest时的div
 *
 * 使用:
   <div id="superinput" class="fsg21" style="cursor:text;height:23px;" onclick="fs_superOnclick()">
	&nbsp;
   </div>
 
   function _bodyonload()
   {
   	fs_superView();
   }
   
   //fs_suggestpars;
   //fs_selectpars;
 */
var fs_selectpars = "gf_me";
var fs_mode = 0;	//0/1 -- 是否支持输入站外名称
var fs_data = [];
var fs_dirty = false;
var fs_maxcount = 30;
var fs_alert = false;
var fs_gindex = 0;
function fs_checkCount(v_alert)
{
	var c = 0;
	var len = fs_data.length;
	for(var i=0; i<len ; i++)
	{
		if(fs_data[i].type == "static")
		{
			c++;
		}
	}
	if (c >= fs_maxcount)
	{
		fs_alert = v_alert;
	}
	return c < fs_maxcount;
}

function fs_hasActive()
{
	var len = fs_data.length;
	for(var i=0; i<len ; i++)
	{
		if(fs_data[i].type == "active")
		{
			return i;
		}
	}
	return -1;
}

function fs_superOnclick()
{
	//$('fsg_nr').style.display = "none";
	if(fs_hasActive() != -1)
	{
		if($("active").value != "" && $("suggest").style.display == "none")
		{
			$("active").value = "";
		}
		$("active").focus();
	}
	else
	{
		fs_data[fs_data.length] = {type:"active"};
		fs_superView();
	}
	$("superinput").parentNode.className = "it2";
}

function fs_activeFocus()
{
	for(var i=0 ; i<5 ; i++)
	{
		if($("active"))
		{
			$("active").focus();
		}
	}
}

function fs_superView()
{
	var html = "";
	var len = fs_data.length;
	for(var i=0; i<len ; i++)
	{
		if(fs_data[i].type == "static")
		{
			if (fs_data[i].UserId == "0" || fs_data[i].UserId == 0)
			{
				html += '<div class="fsg_hy" style="background:#fff9d7;">'+fs_data[i].FullName+'</div>';
			}
			else
			{
				html += '<div class="fsg_hy">'+fs_data[i].FullName+'&nbsp;&nbsp;'+fs_logo20(fs_data[i].FullName)+'</div>';
			}
		}
		else
		{
			html += '<div class="fsg_id" id="activediv" style="width:50px;"><input onkeydown="return fs_inputOnkeydown(event)" onkeyup="fs_inputOnkeyup(event)" onblur="fs_inputOnblur(this);" onfocus="fs_inputOnfocus(this);" id="active" name="active" type="text" value="" class="fsg_it" size="2" Autocomplete="off" maxlength=50/><div class="fsg_nl" id="suggest" style="display:none;width:210px;"></div><div id="emptysuggest" class="fsg_nl" style="padding-left:7px;background:#eeeeee;color:#666666;width:220px;">请输入好友的姓名(开心网上姓名)</div></div></div>';
		}
	}
	$("superinput").innerHTML = html==""?"&nbsp;":html;
	
	
	var childs = $("superinput").childNodes;
	var mintop = 1000000;
	var maxbottom = 0;
	var len = childs.length;
	for(var i=0; i<len ; i++)
	{
		if(childs[i].className == "fsg_hy" || childs[i].className == "fsg_id")
		{
			var pos = getpos(childs[i]);
			if(pos.top <  mintop)
			{
				mintop = pos.top;
			}
			if(pos.bottom > maxbottom)
			{
				maxbottom = pos.bottom;
			}
		}
	}
	var height = maxbottom-mintop;
	height = height<23?23:height;
	$("superinput").style.height = height + "px";
	fs_activeFocus();

	if (fs_alert)
	{
		fs_alert = false;
		alert("最多只能选择" + fs_maxcount + "个用户！");
	}
}


function fs_getHotNum()
{
	var obj;
	var num=0;
	var hotnum = -1;
	while((obj = $("suggest_"+num)) != null)
	{
		if(obj.className == "sgt_on")
		{
			hotnum = num;
		}
		num++;
	}
	return {"hotnum":hotnum,"totalnum":num};
}

function fs_inputOnblur(thisobj)
{
	if($("active") && $("active").value != "")
	{
		fs_getUser();
		
		$("active").value = "";

		$("active").blur();
	}
	$("superinput").parentNode.className = "it1";
	$("suggest").style.display = "none";
	$("emptysuggest").style.display = "none";
}

function fs_inputOnfocus(thisobj)
{
	if(thisobj.value == "")
	{
		$("emptysuggest").style.display = "block";
		$("emptysuggest").innerHTML = "请输入好友的姓名(支持拼音首字母输入)";
	}
}


function fs_inputOnkeydown(evnt)
{
	$("active").style.width = b_strlen($("active").value)*6+20 + "px";
	if (evnt.keyCode == 13)
	{
		return false;
	}
	var activenum = fs_hasActive();
	//BACKSPACE
	if(evnt.keyCode == 8 && fs_data[activenum-1] && $("active").value=="")
	{
		var fs_data2 = [];
		var j=0;
		var len = fs_data.length;
		for(var i=0; i<len ; i++)
		{
			if(activenum-1 == i)
			{
				continue;
			}
			fs_data2[j] = fs_data[i];
			j++;
		}
		fs_data = fs_data2;
		fs_dirty = true;
		fs_superView();
		return;
	}

	//LEFT
	if(evnt.keyCode == 37 && fs_data[activenum-1] && $("active").value=="")
	{
		return;
		var obj = fs_data[activenum];
		fs_data[activenum] = fs_data[activenum-1];
		fs_data[activenum-1] = obj;
		fs_superView();
		return;
	}
	//RIGHT
	if(evnt.keyCode == 39 && fs_data[activenum+1] && $("active").value=="")
	{
		return;
		var obj = fs_data[activenum];
		fs_data[activenum] = fs_data[activenum+1];
		fs_data[activenum+1] = obj;
		fs_superView();
		return;
	}
	
	
	var hotinfo = fs_getHotNum();
	var hotnum = hotinfo.hotnum;
	var num = hotinfo.totalnum;
	//DOWN
	if(evnt.keyCode == 40)
	{
		if($("suggest_0") != null && $("suggest").style.display == "block")
		{
			if(hotnum == -1)
			{
				$("suggest_0").className = "sgt_on";
			}
			else
			{
				var nextnum = hotnum == num-1 ? 0 : hotnum+1;
				$("suggest_"+hotnum).className = "sgt_of";
				$("suggest_"+nextnum).className = "sgt_on";
			}
			return false;
		}
	}
	//UP
	if(evnt.keyCode == 38)
	{
		if($("suggest_0") != null && $("suggest").style.display == "block")
		{
			if(hotnum == -1)
			{
				$("suggest_"+(num-1)).className = "sgt_on";
			}
			else
			{
				var prevnum = hotnum == 0 ? num-1 : hotnum-1;
				$("suggest_"+hotnum).className = "sgt_of";
				$("suggest_"+prevnum).className = "sgt_on";
			}
		}
	}

}

function fs_getUser()
{
	var hotinfo = fs_getHotNum();
	var hotnum = hotinfo.hotnum;
	var totalnum = hotinfo.totalnum;
	var hasuser = $("suggest").style.display == "block" && hotnum!=-1 && totalnum>0;
	if (fs_mode == 1 && !hasuser && $("active").value.length)
	{
		if (fs_checkCount(true))
		{
			var escape_FullName = $("active").value.replace(/&/g, "&amp;");
			escape_FullName = escape_FullName.replace(/</g, "&lt;");
			escape_FullName = escape_FullName.replace(/>/g, "&gt;");
			var friendobj = {UserId:"0",FullName:escape_FullName,FullName_unsafe:$("active").value,type:"static"};

			var activenum = fs_hasActive();
			for(var i=fs_data.length ; i> activenum ; i--)
			{
				fs_data[i] = fs_data[i-1];
			}
			fs_data[activenum] = friendobj;
			fs_dirty = true;
		}
		
		fs_superView();
	}
	else if (hasuser)
	{
		if (fs_checkCount(true))
		{
			var friendobj = fs_frienddata[hotnum];
			friendobj.type = "static";
		
			var activenum = fs_hasActive();
			for(var i=fs_data.length ; i> activenum ; i--)
			{
				fs_data[i] = fs_data[i-1];
			}
			fs_data[activenum] = friendobj;
			fs_dirty = true;
		}
		
		fs_superView();
	}
}

function fs_inputOnkeyup(evnt)
{
	//ENTER
	if (evnt.keyCode == 13)
	{
		fs_getUser();
	}
	if(evnt.keyCode == 38 || evnt.keyCode == 40||$("active").value == "")
	{
	}
	else
	{
		fs_ajax_submit();
	}

}


function fs_ajax_submit()
{
//	var url = "/interface/suggestfriend.php";
//	var text = encodeURIComponent($("active").value);
//	var pars = "text=" + text + "&uid={_UID_}";
//	if("undefined" != typeof fs_suggestpars)
//	{
//		pars += "&pars="+fs_suggestpars;
//	}
    //	var myAjax = new Ajax.Request(url, {method: "get", parameters: pars, onComplete: function (req) { fs_ajax_show(req); } });
    var text = encodeURIComponent($("active").value);
    SelectFriendsService.GetNameListAsJson(text, fs_ajax_show);
}

var fs_frienddata = [];
function fs_ajax_show(req) {
	var arr = req;   
	fs_frienddata = arr;
	
	if(arr.length == 0)
	{
		$("suggest").style.display = "none";
		$("emptysuggest").style.display = "block";
		if($("active").value == "")
		{
			$("emptysuggest").innerHTML = "请输入好友的姓名(支持拼音首字母输入)";
		}
		else
		{
			if (fs_mode == 1)
			{
				$("emptysuggest").style.display = "none";
			}
			else
			{
				$("emptysuggest").innerHTML = "姓名不在好友列表哦，请重新输入";
			}
		}
		return;
	}
	
	var html = "";
	for(var i=0 ; i<arr.length ; i++)
	{
		html += '<div id=suggest_'+i+' class="sgt_of" style="width:200px;" onmouseover="fs_suggestOnmouseover(this)" onmousedown="fs_suggestOnmousedown(this);">'+arr[i].FullName+'　'+fs_logo20(arr[i].UserId)+'</div>';
	}
	$("suggest").innerHTML = html;
	$("suggest").style.display = "block";
	$("emptysuggest").style.display = "none";
	
	if($("suggest_0") != null && $("suggest").style.display == "block")
	{
		$("suggest_0").className = "sgt_on";
	}
	
}

function fs_suggestOnmouseover(thisobj)
{
	var arr = thisobj.id.split('_');
	var thisnum = arr[1];
	
	var obj;
	var num=0;
	while((obj = $("suggest_"+num)) != null)
	{
		if(thisnum == num)
		{
			obj.className = "sgt_on";
		}
		else
		{
			obj.className = "sgt_of";
		}
		num++;
	}
	
}

function fs_suggestOnmousedown(thisobj)
{
	if (fs_checkCount(true))
	{
		var arr = thisobj.id.split('_');
		var num = arr[1];
		var friendobj = fs_frienddata[num];
		friendobj.type = "static";

		var activenum = fs_hasActive();
		for(var i=fs_data.length ; i> activenum ; i--)
		{
			fs_data[i] = fs_data[i-1];
		}
		fs_data[activenum] = friendobj;
		fs_dirty = true;
	}
	
	fs_superView();
}

function  fs_viewFriend()
{
	
	var count = 0;
	var len = fs_allfrienddata.length;
	for(var i=0; i<len ; i++)
	{
		if($("checkbox"+i).checked)
		{
			count++;
		}
	}
	var confirmret = 0;
	if(count==0 || ((count > 0) && (confirmret = confirm("切换分组后将清空你刚才勾选的好友，你确定要切换分组吗？"))))
	{
		var url = "/interface/suggestfriend.php";
		var pars = "type=all";
		if($("group"))
		{
			pars = pars +"&group="+ $("group").value;
		}
		if("undefined" != typeof fs_selectpars)
		{
			pars += "&pars="+fs_selectpars;
		}
		var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { fs_ajax_allfriendshow(req); } });
		
		$("fsg_nr").style.display = "block";
		$('xx_sh').innerHTML = '<img src="../i/xx_xs1.gif" class="cp" onmouseover="this.src=\'../i/xx_xs2.gif\';" onmouseout="this.src=\'../i/xx_xs1.gif\';" alt="选择好友" />';
		if($("group"))
		{
			fs_gindex = $("group").selectedIndex;
		}
	}
	else if(!confirmret) //如果是取消
	{
		if($("group"))
		{
			$("group").selectedIndex = fs_gindex;
		}
	}
}


//all friend
function fs_viewAllfriend()
{
	if($("groupsel") && (!$("group")))
	{
		var url = "/interface/fgroup.php";
		var pars = "";
		var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (req) { fs_ajax_groupshow(req); } });
	}
	
	
	if($("suggest"))
	{
		$("suggest").style.display = "none";
	}

	if($('fsg_nr').style.display == "block")
	{
		$('fsg_nr').style.display = "none";
		$('xx_sh').innerHTML = '<img src="../i/xx_xx1.gif" class="cp" onmouseover="this.src=\'../i/xx_xx2.gif\';" onmouseout="this.src=\'../i/xx_xx1.gif\';" alt="选择好友" />';
	}
	else
	{
		fs_viewFriend();
	}
}

function fs_ajax_groupshow(req)
{
	var arr = eval(req.responseText);
	var html = '<select id="group" name="group" onchange="fs_viewFriend();">';
	html = html +  '<option value="">全部好友</option>';
	var len = arr.length;
	for(var i=0; i<len; i++)
	{
		html = html + ' <option value="' +arr[i]+ '">'+arr[i]+'</option>';
	}
	html = html + '</select>';

	$("groupsel").innerHTML = html;
	
}

var fs_allfrienddata = [];
function fs_ajax_allfriendshow(req)
{
	var arr = eval(req.responseText);
	fs_allfrienddata = arr;
	var html = "";
	var len = Math.ceil(arr.length/3)*3;
	for(var i=0 ; i<len ; i++)
	{	
		if(i%3 == 0)
		{
			html += '<div class="sgt_of" style="width:300px;">\n';
		}
		if(arr[i])
		{
			html += '<div class="l" style="width:100px;" title="开心ID：'+arr[i].UserId+'"><input id="checkbox'+i+'" type="checkbox" title="开心ID：'+arr[i].UserId+'" onclick="fs_countcheck(this);" />'+arr[i].FullName+fs_logo20(arr[i])+'</div>\n';
		}
		if(i%3 == 2)
		{
			html += '<div class="c"></div>\n';
			html += '</div>\n';
		}
	}
	$("allfriend").innerHTML = html;
	if(arr.length > fs_maxcount)
	{
		$("selall").disabled = true;
	}
	else
	{
		$("selall").disabled = false;
	}
}
function fs_selectAll()
{
	var checked = $("selall").checked;
	var len = fs_allfrienddata.length;
	for(var i=0; i<len ; i++)
	{
		
		$("checkbox"+i).checked = checked;
	}
}
function fs_countcheck(obj)
{
	var count = 0;
	var len = fs_allfrienddata.length;
	for(var i=0; i<len ; i++)
	{
		if($("checkbox"+i).checked)
		{
			count++;
			if(count > fs_maxcount)
			{
				obj.checked = false;
				alert("最多只能选择 "+fs_maxcount+" 位好友！");
				break;
			}
		}
	}
	return count;
}

function fs_selectFriend()
{
	var len = fs_allfrienddata.length;
	for(var i=0; i<len ; i++)
	{
		if($("checkbox"+i).checked)
		{
			var obj = fs_allfrienddata[i];
			obj.type = "static";
			if (!fs_checkCount(true))
			{
				break;
			}
			fs_data[fs_data.length] = fs_allfrienddata[i];
		}
	}
	var fs_data2 = [];
	var j=0;
	len = fs_data.length;
	for(var i=0; i<len ; i++)
	{
		if(fs_data[i].type == "active")
		{
			continue;
		}
		
		fs_data2[j] = fs_data[i];
		j++;
	}
	fs_data = fs_data2;
	fs_dirty = true;

	$('fsg_nr').style.display = "none";
	$('xx_sh').innerHTML = '<img src="../i/xx_xx1.gif" class="cp" onmouseover="this.src=\'../i/xx_xx2.gif\';" onmouseout="this.src=\'../i/xx_xx1.gif\';" alt="选择好友" />';

	fs_superView();
}

function fs_logo20(obj)
{
	var logo20 = "";
	if(typeof obj.logo20 != "undefined")
	{
		logo20 = "<img src='"+obj.logo20+".jpg'  align=absmiddle width=15  />";
	}
	return logo20;
}

